﻿using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System; using Extra.ERP;
using System.Collections;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms; using Common;

namespace HRM.CORE.CLASS
{
    public class CreateHeaderReport
    {
        private static string m_CompanyAddress;
        private static string m_CompanyName;
        private static Font m_FontCompanyAddress;
        private static Font m_FontCompanyName;
        private static Image m_ImageHeader;
        private static float m_ImageHeight;
        private static float m_ImageWidth;
        private static bool m_IsShowLogo;
        private static bool m_IsUperCompanyName;
        private static Image m_Logo;
        private static int m_Style;
        private static string xmlFormatOptionPath = (Application.StartupPath + @"\Layout\headerReportFormatOption.xml");

        public static void CreateHeader(XRControl Band)
        {
            LoadFormatOption();
            XRPictureBox child = new XRPictureBox();
            XRLabel label = new XRLabel();
            XRLabel label2 = new XRLabel();
            XRPictureBox box2 = new XRPictureBox();
            float heightF = 0f;
            if (m_Style == 0)
            {
                LoadCompanyInformation();
                if (m_IsShowLogo)
                {
                    child.Image = m_Logo;
                    child.WidthF = 100f;
                    child.HeightF = 100f;
                    child.Sizing = ImageSizeMode.ZoomImage;
                }
                else
                {
                    child.WidthF = 0f;
                    child.HeightF = 0f;
                }
                Band.Controls.Add(child);
                label.Text = m_CompanyName;
                label.Font = m_FontCompanyName;
                label.LeftF = (child.LeftF + child.WidthF) + 10f;
                label.TopF = ((child.HeightF / 2f) - label.HeightF) - (label.HeightF / 4f);
                label.WidthF = 500f;
                Band.Controls.Add(label);
                label2.Text = m_CompanyAddress;
                label2.Font = m_FontCompanyAddress;
                label2.LeftF = (child.LeftF + child.WidthF) + 10f;
                label2.TopF = (label.TopF + label.HeightF) + 10f;
                label2.WidthF = 500f;
                Band.Controls.Add(label2);
                if (m_IsShowLogo)
                {
                    heightF = child.HeightF;
                }
                else
                {
                    heightF = label2.TopF + label2.HeightF;
                }
            }
            else
            {
                box2.Image = m_ImageHeader;
                box2.WidthF = m_ImageWidth;
                box2.HeightF = m_ImageHeight;
                box2.Sizing = ImageSizeMode.AutoSize;
                Band.Controls.Add(box2);
                heightF = box2.HeightF;
            }
            foreach (XRControl control in (IEnumerable) Band)
            {
                if ((((control != child) && (control != box2)) && (control != label)) && (control != label2))
                {
                    control.TopF = heightF + 10f;
                    heightF = (heightF + control.HeightF) + 4f;
                }
            }
            Band.HeightF += 10f;
        }

        private static void LoadCompanyInformation()
        {
            //if (MyInfo.GetInfo() == "OK")
            //{
            //    if (m_IsUperCompanyName)
            //    {
            //        m_CompanyName = MyInfo.Company.ToUpper();
            //    }
            //    else
            //    {
            //        m_CompanyName = MyInfo.Company;
            //    }
            //    m_CompanyAddress = MyInfo.Address;
            //    m_Logo = MyInfo.Photo;
            //}
        }

        private static void LoadFormatOption()
        {
            FontConverter converter = new FontConverter();
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(xmlFormatOptionPath);
                m_Style = int.Parse(set.Tables[0].Rows[0]["Style"].ToString());
                m_IsShowLogo = bool.Parse(set.Tables[0].Rows[0]["IsShowLogo"].ToString());
                m_IsUperCompanyName = bool.Parse(set.Tables[0].Rows[0]["IsUperCompanyName"].ToString());
                m_FontCompanyName = converter.ConvertFromString(set.Tables[0].Rows[0]["FontCompanyName"].ToString()) as Font;
                m_FontCompanyAddress = converter.ConvertFromString(set.Tables[0].Rows[0]["FontCompanyAddress"].ToString()) as Font;
                try
                {
                    byte[] buffer = Convert.FromBase64String(set.Tables[0].Rows[0]["ImageHeader"].ToString());
                    if (buffer.Length != 2)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        m_ImageHeader = Image.FromStream(stream);
                    }
                    if (buffer.Length == 2)
                    {
                        m_ImageHeader = null;
                    }
                }
                catch
                {
                    m_ImageHeader = null;
                }
                m_ImageWidth = float.Parse(set.Tables[0].Rows[0]["ImageWidth"].ToString());
                m_ImageHeight = float.Parse(set.Tables[0].Rows[0]["ImageHeight"].ToString());
            }
            catch
            {
                m_Style = 0;
                m_IsShowLogo = true;
                m_IsUperCompanyName = true;
                m_FontCompanyName = converter.ConvertFromString("Times New Roman, 11,25pt, style=Bold") as Font;
                m_FontCompanyAddress = converter.ConvertFromString("Times New Roman, 11,25pt, style=Regular") as Font;
                m_ImageHeader = null;
                m_ImageWidth = 500f;
                m_ImageHeight = 100f;
            }
        }
    }
}

